package com.yunbao.hujin.mymvptest.login;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.yunbao.hujin.mymvptest.R;
import com.yunbao.hujin.mymvptest.login.presenter.ILoginPresenter;
import com.yunbao.hujin.mymvptest.login.presenter.LoginPresenterCompl;
import com.yunbao.hujin.mymvptest.login.view.ILoginView;

public class LoginActivity extends AppCompatActivity implements ILoginView{

    private EditText editUser;
    private EditText editPass;
    private Button btnLogin;
    private Button   btnClear;
    private ProgressBar progressBar;

    private ILoginPresenter loginPresenter;     //loginPresenter接口，其逻辑实现在presenterCompl里


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        findView();
        setListener();
    }

    private void findView() {
        //find view
        editUser = (EditText) this.findViewById(R.id.et_login_username);
        editPass = (EditText) this.findViewById(R.id.et_login_password);
        btnLogin = (Button) this.findViewById(R.id.btn_login_login);
        btnClear = (Button) this.findViewById(R.id.btn_login_clear);
        progressBar = (ProgressBar) this.findViewById(R.id.progress_login);

        //init
        /**
         * 其实activity不必先实现ILoginView接口，在此处实例化presenter时
         * 【presenter里包含ILoginView接口】传进this参数，则此activity必须实现ILoginView接口
         */
        loginPresenter = new LoginPresenterCompl(this);
        loginPresenter.setProgressBarVisiblity(View.INVISIBLE);
    }

    private void setListener() {
        btnLogin.setOnClickListener(v -> {
            btnLogin.setEnabled(false);
            btnClear.setEnabled(false);
            loginPresenter.setProgressBarVisiblity(View.VISIBLE);
            loginPresenter.doLogin(editUser.getText().toString(), editPass.getText().toString());
        });

        btnClear.setOnClickListener(v->{
            loginPresenter.clear();//在此处实现presenter中的clear()方法【clear()又在LoginPresenterCompl类中执行，】
            //之后再LoginPresenterCompl实现的clear()方法里实现ILoginView接口的onClearText()【onClearText()又在此Activity中执行】
        });
    }


    /*******   -------ILoginView接口实现-------      ********/
    @Override
    public void onClearText() {
        editUser.setText("");
        editPass.setText("");
    }

    @Override
    public void onLoginResult(Boolean result, int code) {
        //只是处理登录返回结果---具体登录逻辑在presenterCompl中实现
        loginPresenter.setProgressBarVisiblity(View.INVISIBLE);
        btnLogin.setEnabled(true);
        btnClear.setEnabled(true);
        if (result) {
            Toast.makeText(this, "Login Success", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, "Login Fail, code = " + code, Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onSetProgressBarVisibility(int visibility) {
        progressBar.setVisibility(visibility);
    }

}
